package com.icss.mqsp.quartz.trigger;

import com.icss.mqsp.quartz.job.CacheJob;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;
import java.util.HashMap;
import java.util.Map;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CacheTriggerRunner
{
  Log log = LogFactory.getLog(CacheTriggerRunner.class);
  private static Map map = new HashMap();
  
  public void task()
    throws SchedulerException
  {
    SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    
    Scheduler scheduler = schedulerFactory.getScheduler();
    
    JobDetail cacheDetail = new JobDetail("Cache", "jobDetailGroup", CacheJob.class);
    this.log.info("作业初始化成功......");
    
    CronTrigger cacheTrigger = new CronTrigger("CacheTrigger", "triggerGroup");
    this.log.info("触发器初始化成功......");
    try
    {
      CronExpression cexp = new CronExpression("0 30 5-18 * * ?");
      
      cacheTrigger.setCronExpression(cexp);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    scheduler.scheduleJob(cacheDetail, cacheTrigger);
    this.log.info("绑定成功......");
    
    map.put("scheduler", scheduler);
    scheduler.start();
  }
  
  public void shutdown()
  {
    Scheduler scheduler = (Scheduler)map.get("scheduler");
    try
    {
      scheduler.shutdown();
    }
    catch (SchedulerException e)
    {
      e.printStackTrace();
      this.log.error(e.toString());
    }
  }
}
